package com.wangyp.tower.tower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangyp.tower.model.tower.TowerInfo;
import com.wangyp.tower.tower.mapper.TowerInfoMapper;
import com.wangyp.tower.tower.service.TowerInfoService;
import com.wangyp.tower.vo.tower.TowerInfoQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.Date;

/**
 * @Author: Wangyp
 * @Date: 2021/4/25 11:26
 * @Description: *
 */
@Service
public class TowerInfoServiceImpl extends ServiceImpl<TowerInfoMapper, TowerInfo> implements TowerInfoService {

    /**
     * 抽取各种情况分页的公共部分
     * @param current
     * @param limit
     * @param towerInfoQueryVo
     * @param status
     * @return Page<TowerInfo>
     */
    private Page<TowerInfo> findTowerinfo(long current, long limit, TowerInfoQueryVo towerInfoQueryVo, Boolean status) {

        Page<TowerInfo> page = new Page<>(current, limit);
        QueryWrapper<TowerInfo> wrapper = new QueryWrapper<>();

        String ownerType = towerInfoQueryVo.getOwnerType();
        String towerId = towerInfoQueryVo.getTowerId();
        String provinceCode = towerInfoQueryVo.getProvinceCode();
        String regionCode = towerInfoQueryVo.getRegionCode();
        String countyCode = towerInfoQueryVo.getCountyCode();

        if (!StringUtils.isEmpty(ownerType)) {
            wrapper.like("owner_type", ownerType);
        }
        if (!StringUtils.isEmpty(towerId)) {
            wrapper.eq("tower_id", towerId);
        }
        if (!StringUtils.isEmpty(provinceCode)){
            wrapper.eq("province_code", provinceCode);
        }
        if (!StringUtils.isEmpty(regionCode)){
            wrapper.eq("region_code",regionCode);
        }
        if (!StringUtils.isEmpty(countyCode)){
            wrapper.eq("county_code", countyCode);
        }
        wrapper.eq("status", status);
        return page(page, wrapper);
    }


    /**
     * 分页查询
     * @param current
     * @param limit
     * @param towerInfoQueryVo
     * @return Page<TowerInfo>
     */
    @Override
    public Page<TowerInfo>  findPageTowerInfo(long current, long limit, String name, TowerInfoQueryVo towerInfoQueryVo) {

        Page<TowerInfo> page = new Page<>(current, limit);
        QueryWrapper<TowerInfo> wrapper = new QueryWrapper<>();
        System.out.println(name);
        wrapper.eq("owner_name", name);

        String ownerType = towerInfoQueryVo.getOwnerType();
        String towerId = towerInfoQueryVo.getTowerId();
        String provinceCode = towerInfoQueryVo.getProvinceCode();
        String regionCode = towerInfoQueryVo.getRegionCode();
        String countyCode = towerInfoQueryVo.getCountyCode();

        if (!StringUtils.isEmpty(ownerType)) {
            wrapper.like("owner_type", ownerType);
        }
        if (!StringUtils.isEmpty(towerId)) {
            wrapper.eq("tower_id", towerId);
        }
        if (!StringUtils.isEmpty(provinceCode)){
            wrapper.eq("province_code", provinceCode);
        }
        if (!StringUtils.isEmpty(regionCode)){
            wrapper.eq("region_code",regionCode);
        }
        if (!StringUtils.isEmpty(countyCode)){
            wrapper.eq("county_code", countyCode);
        }
        return page(page, wrapper);
    }

    /**
     * 分页查询启用铁塔
     * @param current
     * @param limit
     * @param towerInfoQueryVo
     * @return Page<TowerInfo>
     */
    @Override
    public Page<TowerInfo> findPageEnableTowerInfo(long current, long limit, TowerInfoQueryVo towerInfoQueryVo) {
        return findTowerinfo(current, limit, towerInfoQueryVo, true);
    }


    /**
     * 分页查询未启用铁塔
     * @param current
     * @param limit
     * @param towerInfoQueryVo
     * @return Page<TowerInfo>
     */
    @Override
    public Page<TowerInfo> findPageNotEnableTowerInfo(long current, long limit, TowerInfoQueryVo towerInfoQueryVo) {
        return findTowerinfo(current, limit, towerInfoQueryVo, false);
    }

    /**
     * 模糊查询铁塔(省份 地市 区县 产权方)
     * @param towerInfoQueryVo
     * @return
     */
    @Override
    public TowerInfo getByCountyCode(TowerInfoQueryVo towerInfoQueryVo) {

        QueryWrapper<TowerInfo> wrapper = new QueryWrapper<>();

        String ownerType = towerInfoQueryVo.getOwnerType();
        String provinceCode = towerInfoQueryVo.getProvinceCode();
        String regionCode = towerInfoQueryVo.getRegionCode();
        String countyCode = towerInfoQueryVo.getCountyCode();
        Date activeTime = towerInfoQueryVo.getActiveTime();
        Date inactiveTime = towerInfoQueryVo.getInactiveTime();

        if (!StringUtils.isEmpty(ownerType)) {
            wrapper.like("owner_type", ownerType);
        }
        if (!StringUtils.isEmpty(provinceCode)){
            wrapper.eq("province_code", provinceCode);
        }
        if (!StringUtils.isEmpty(regionCode)){
            wrapper.eq("region_code",regionCode);
        }
        if (!StringUtils.isEmpty(countyCode)){
            wrapper.eq("county_code", countyCode);
        }
        if (!StringUtils.isEmpty(activeTime)){
            wrapper.eq("activeTime",activeTime);
        }
        if (!StringUtils.isEmpty(inactiveTime)){
            wrapper.eq("inactiveTime", inactiveTime);
        }
        return baseMapper.selectOne(wrapper);
    }

    /**
     * 添加铁塔信息
     * @param towerInfo
     * @return
     */
    @Override
    public boolean addTowerInfo(TowerInfo towerInfo) {
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        towerInfo.setActiveTime(date);

        calendar.add(Calendar.YEAR, 2);
        date = calendar.getTime();
        towerInfo.setInactiveTime(date);
        towerInfo.setStatus(0);


        return save(towerInfo);
    }
}
